#include <iostream>
using namespace std;

/* 文件类 */
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <ostream>
#include <string>

#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>   // 包含 write 函数的头文件
#include <time.h>     // 包含 sleep 函数的头文件
#include <ctype.h>
/* 得到CPU的利用率 和 内存利用率 */
int main()
{
    //system("cat /proc/meminfo | grep MemTotal");
    /* 读文件 */
    ifstream ifs;

    /* 打开文件 */
    ifs.open("/proc/meminfo");//无返回值

    /* */
    if(!ifs.is_open())
    {
        /* 打开文件失败 */
        exit(-1);
    }

    /* 打开文件成功 */
    char buffer[32] = { 0 };

    int totalMem = 0;
    int freeMem = 0;

#if 1
    while(!ifs.eof())
    {
        
        /* 读取 一行 */
        ifs.getline(buffer, sizeof(buffer));
        if((strstr(buffer, "MemTotal") != NULL))
        {
            string lineData(buffer);

            int startPos = 0;
            while(!isdigit(lineData[startPos]))
            {
                startPos++;
            }
            int endLen = 0;
            while(isdigit(lineData[startPos + endLen]))
            {
                endLen++;
            }

            totalMem = atoi(lineData.substr(startPos, endLen).c_str());
        }

        if((strstr(buffer, "MemAvailable") != NULL))
        {
            string lineData(buffer);

            int startPos = 0;
            while(!isdigit(lineData[startPos]))
            {
                startPos++;
            }
          
            int endLen = 0;
            while(isdigit(lineData[startPos + endLen]))
            {
                endLen++;
            }

            freeMem = atoi(lineData.substr(startPos, endLen).c_str());

        }
    }
    
    cout << "totalMem:" << totalMem << endl;
    cout << "freeMem:" << freeMem << endl;

    float usage = (float)(freeMem * 100.0 / totalMem);
    cout << "usage:" << usage << endl;
#else 
    while (std::getline(ifs, line)) 
    {
        if (line.find("MemTotal") != std::string::npos) 
        {
            cout << line << endl;
            break;
        }
    }
#endif
    /* 创建套接字 */
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);

    /* 绑定远程服务器的IP 和 端口 */
    struct sockaddr_in  remoteAddress;
    memset(&remoteAddress, 0, sizeof(remoteAddress));//一定要memset一下
    remoteAddress.sin_family = AF_INET;
    remoteAddress.sin_port = htons(8080);   // 要转成大端
    //const char * publicIp = "112.124.68.184";
    const char * publicIp = "121.40.174.165";
    inet_pton(AF_INET, publicIp, (void *)&remoteAddress.sin_addr.s_addr); 
    connect(sockfd,  (struct sockaddr *)&remoteAddress, sizeof(remoteAddress));
    
    /* 数据上报 */
    char data[64] = { 0 };
    while (1)
    {
        #if 0 
        /* 格式化字符串 */
        snprintf(data, sizeof(data), "memUsage : %.2f%%", usage);
        write(sockfd, data, sizeof(data));
        sleep(3);
        memset(data, 0, sizeof(data)); 
        #endif

        std::cout << " 请输入信息 ";
        cin>>data;

        write(sockfd, data, strlen(data));
    }

    return 0;
}